#include "i2c.h"
#include "accel.h"

#define SADDR 0x1d

//读寄存器
uint8_t accel_read(uint8_t reg)
{
  uint8_t data;
  i2c_write(SADDR, &reg, 1);
  i2c_read(SADDR, &data, 1);
  return data;
}

//写寄存器
//reg: 寄存器地址
//data: 写入数据
void accel_write(uint8_t reg, uint8_t data)
{
  uint8_t buf[2];
  buf[0] = reg;
  buf[1] = data;
  i2c_write(SADDR, buf, 2);
}

void accel_init(void)
{
  //设置为测量模式，范围2G
  accel_write(0x16, 5);
}

int8_t accel_x(void)
{
  return accel_read(0x6);
}

int8_t accel_y(void)
{
  return accel_read(0x7);
}

int8_t accel_z(void)
{
  return accel_read(0x8);
}
